<?php
/*由脚本生成 powerBy--Reer*/

namespace app\common\manage;

use app\common\bean\GlobalBean;
use app\common\bean\ListMap;
use app\common\bean\StorePermissionBean;
use app\common\bean\StoreRoleBean;
use app\common\bean\StoreRolePermissionBean;
use app\common\exception\MyException;
use app\common\model\StorePermissionModel;
use app\common\model\StoreRoleModel;
use app\common\model\StoreRolePermissionModel;
use app\common\utils\PermissionUtil;
use app\common\validate\StoreRolePermissionValidate;
use think\Model;

class StoreRolePermissionManage
{
	protected static $_self = null;
	static function getInstance(){
		if(empty(self::$_self)){
			self::$_self = new StoreRolePermissionManage();
		}
		return self::$_self;
	}

    /** 获取场馆角色权限树
     * @param StoreRolePermissionBean $storeRolePermissionBean
     * @return array
     * @throws \Exception
     */
    public function getStoreRolePermissionTree(StoreRolePermissionBean $storeRolePermissionBean){
        $allAdminRolePermission = $this->getRolePermissionlist($storeRolePermissionBean);
        session(GlobalBean::$permissionList,$allAdminRolePermission);
        $permissionTree = PermissionUtil::getInstance()->getTreesData($allAdminRolePermission);
        return $permissionTree;
    }

    /** 获取角色拥有的权限列表
     * @param StoreRolePermissionBean $storeRolePermissionBean
     * @return ListMap|bool
     * @throws \Exception
     */
    private function getRolePermissionlist(StoreRolePermissionBean $storeRolePermissionBean){
        StoreRolePermissionValidate::getInstance()->goCheck(StoreRolePermissionValidate::$storeRoleId,$storeRolePermissionBean->getData());
        $this->checkDataPermssion(StoreRoleBean::$id,$storeRolePermissionBean->getStoreRoleId(),StoreRoleModel::getInstance(),StoreRoleBean::$storeId,session(GlobalBean::$storeId));
        if($storeRolePermissionBean->getStoreRoleId() == GlobalBean::$superStoreRoleId){
            $allAdminRolePermission = StorePermissionModel::getInstance()->selectData([],true,StorePermissionBean::$sort);
        }else{
            $mixedMap = new ListMap();
            $mixedMap->setParameter('alias',StoreRolePermissionBean::$alias);
            $mixedMap->setParameter('field',StorePermissionBean::$alias.POINST."*");
            $mixedMap->setParameter('order',StorePermissionBean::$alias.POINST.StorePermissionBean::$parentId.",".StorePermissionBean::$alias.POINST.StorePermissionBean::$sort);
            $mixedMap->setParameter('join',array(StorePermissionBean::$tableName,StorePermissionBean::$alias,StorePermissionBean::$alias.POINST.StorePermissionBean::$id,StoreRolePermissionBean::$alias.POINST.StoreRolePermissionBean::$storePermissionId));
            $mixedMap->setParameter(StoreRolePermissionBean::$alias.POINST.StoreRolePermissionBean::$storeRoleId,$storeRolePermissionBean->getStoreRoleId());
            $allAdminRolePermission = StoreRolePermissionModel::getInstance()->getJoinList($mixedMap,true);
        }
        $allAdminRolePermission !== false ?:MyException::throwMyException("获取权限列表失败");
        return $allAdminRolePermission;
    }

    /** 获取角色权限树详情
     * @param StoreRolePermissionBean $storeRolePermissionBean
     * @return array|bool|false
     * @throws \Exception
     */
    public function getStoreRoleHavePermissionTree(StoreRolePermissionBean $storeRolePermissionBean){
        $rolePermission = $this->getRolePermissionlist($storeRolePermissionBean);
        $allPermission = StorePermissionModel::getInstance()->selectData(array(),true);
        $allPermission = PermissionUtil::getInstance()->twoPermiisionListCheck($allPermission,$rolePermission);
        $permissionTree = PermissionUtil::getInstance()->getTreesData($allPermission);
        return $permissionTree;
    }

    /** 获取全部的权限树、总数
     * @return array
     */
    public function getAllPermissionTree(){
        $allPermission = StorePermissionModel::getInstance()->selectData(array(),true);
        $permissionTree = PermissionUtil::getInstance()->getTreesData($allPermission);
        return array($permissionTree,count($allPermission));
    }

    /** 分配修改角色权限
     * @param StoreRolePermissionBean $storeRolePermissionBean
     * @param $permissionIds
     * @return bool
     * @throws \Exception
     */
    public function allocationPermissions(StoreRolePermissionBean $storeRolePermissionBean,$permissionIds){
        StoreRolePermissionValidate::getInstance()->goCheck(StoreRolePermissionValidate::$storeRoleId,$storeRolePermissionBean->getData());
        $this->checkDataPermssion(StoreRoleBean::$id,$storeRolePermissionBean->getStoreRoleId(),StoreRoleModel::getInstance(),StoreRoleBean::$storeId,session(GlobalBean::$storeId));
        $storeRolePermissionBean->getStoreRoleId() != GlobalBean::$superStoreRoleId ?:MyException::throwMyException("不可修改超级管理权限");
        $storeRolePermissionModel = StoreRolePermissionModel::getInstance();
        $storeRolePermissionModel->startTrans();
        try{
            $temp[StoreRolePermissionBean::$storeRoleId] = $storeRolePermissionBean->getStoreRoleId();
            $data = [];
            $result = $storeRolePermissionModel->deleteData(array(StoreRolePermissionBean::$storeRoleId => $storeRolePermissionBean->getStoreRoleId()));
            $result !== false ?:MyException::throwMyException("删除旧权限失败");
            foreach ($permissionIds as $value){
                $temp[StoreRolePermissionBean::$storePermissionId] = $value;
                $data[] = $temp;
            }
            $storeRolePermissionModel->insertAll($data);
            $storeRolePermissionModel->commit();
        }catch (\Exception $exception){
            $storeRolePermissionModel->rollback();
            MyException::throwMyException($exception->getMessage());
        }
        return true;
    }

    /** 检测是否有权限
     * @param $loginRoleId
     * @param StorePermissionBean $StorePermissionBean
     * @return bool
     */
    public function checkRolePermission($loginRoleId,StorePermissionBean $StorePermissionBean){
        $reuslt = StorePermissionModel::getInstance()->getRolePermissionDetail($loginRoleId,$StorePermissionBean);
        return boolval($reuslt);
    }

    /**
     * 检测客户的数据操作权限
     * @param $Field
     * @param $Id
     * @param $model
     * @param $idField
     * @param $id
     * @return bool
     * @throws \Exception
     */
    public function checkDataPermssion($Field,$Id,$model,$idField,$id){
        if(empty($Id)){//无数据时默认通过
            return true;
        }
        $condition[$Field] = intval($Id);
        $condition[$idField] = intval($id);
        try {
            /** @var Model $model */
            if (!$result = $model->where($condition)->find()) {
                throw MyException::throwMyException("无权限操作该数据");
            }
        } catch (\Exception $e) {
            throw MyException::throwMyException($e->getMessage());
        }
        return $result;
    }
}